package com.yhk.wuchat.common.websocket.domain.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * Description: ws前端请求类型枚举
 **/
@AllArgsConstructor
@Getter
public enum WSReqTypeEnum {
    LOGIN(1, "请求登录二维码"),
    HEARTBEAT(2, "心跳包"),
    AUTHORIZE(3, "登录认证"),
    ;

    private final Integer type;
    private final String desc;

    /**
     * 用map集合来充当缓存
     */
    private static Map<Integer, WSReqTypeEnum> cache;

    /**
     * 在类加载的时候初始化缓存
     */
    static {
        cache = Arrays.stream(WSReqTypeEnum.values()).collect(Collectors.toMap(WSReqTypeEnum::getType, Function.identity()));
    }

    /**
     * 根据type获取枚举
     *
     * @param type
     * @return
     */
    public static WSReqTypeEnum of(Integer type) {
        return cache.get(type);
    }
}
